from django.shortcuts import render
from rest_framework import viewsets
from django.db.models import Q
from .models import Company,Department
from rest_framework.decorators import action
from .serializers import CompanySerializer,DepartmentSerializer
from utils.response_util import VueAPIResponse,APIResponse
from utils.pagination_util import PageNumberPaginationUtil
# Create your views here.
from server.utils import  check_group_id
from rest_framework.decorators import  permission_classes
from rest_framework.permissions import AllowAny


# Create your views here.
@permission_classes((AllowAny,))
class CompanyViewSet(viewsets.ModelViewSet):
    queryset = Company.objects.all()
    serializer_class = CompanySerializer
    pagination_class = PageNumberPaginationUtil

    def list(self, request, *args, **kwargs):
        company_list = self.queryset.all().order_by('id')
        name = request.GET.get('name')
        is_auth = request.GET.get('is_auth')
        company_id = request.GET.get('company_id')

        if hasattr(self.request.user,'group_id'):
            user_group_id = self.request.user.group_id
            if check_group_id(user_group_id) == 'company_admin':
                company_list = company_list.filter(
                    Q(id=self.request.user.company_id))
            if name:
                company_list = company_list.filter(
                    Q(name__icontains=name))
        if is_auth:
            company_list = company_list.filter(
                Q(is_auth=is_auth))
        if company_id:
            company_list = company_list.filter(
                Q(id=company_id))
        page = self.paginate_queryset(company_list)
        serializer = self.get_serializer(company_list, many=True)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
        return APIResponse(results=self.get_paginated_response(serializer.data).data)

    @action(methods=['get'], detail=False, url_path='all')
    def all_list(self, request, *args, **kwargs):
        # 查询普通用户
        queryset = self.get_queryset().filter(is_auth=1)
        serializer = self.get_serializer(queryset, many=True)
        return APIResponse(results=serializer.data)


    @action(methods=['put'], detail=False, url_path='set_company_auth')
    def set_company_auth(self, request, *args, **kwargs):

        # 查询除了超级管理员外的所有用户
        updateData = self.request.data
        id = updateData['id']
        updateData.pop('id')
        Company.objects.filter(id=id).update(**updateData)
        return APIResponse(results={})



class CompanyDepartmentViewSet(viewsets.ModelViewSet):
    queryset = Department.objects.all()
    serializer_class = DepartmentSerializer
    pagination_class = PageNumberPaginationUtil

    def list(self, request, *args, **kwargs):
        department_list = self.queryset.all().order_by('id')

        if check_group_id(self.request.user.group_id) == 'super_admin':
            department_list = self.filter_queryset(self.get_queryset())
        elif check_group_id(self.request.user.group_id) == 'company_admin':
            # 查公司部门
            department_list = self.filter_queryset(self.get_queryset().filter(company_id=self.request.user.company_id))
        else:
            department_list = self.filter_queryset(self.get_queryset())


        name = request.GET.get('name')
        if name:
            department_list = department_list.filter(
                Q(name__icontains=name))
        page = self.paginate_queryset(department_list)
        serializer = self.get_serializer(department_list, many=True)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
        return APIResponse(results=self.get_paginated_response(serializer.data).data)

    @action(methods=['get'], detail=False, url_path='all')
    def all_list(self, request, *args, **kwargs):
        # 查询当前公司部门
        company_id = self.request.user.company_id
        if company_id:
            queryset = self.get_queryset().filter(company_id=company_id)
        else:
            queryset = self.get_queryset().filter()
        serializer = self.get_serializer(queryset, many=True)
        return APIResponse(results=serializer.data)



